package com.ncmmall.mall.controller.business.goods;

import com.ncmmall.application.business.goods.GoodsUnitApplication;
import com.ncmmall.application.securitys.logs.OperationLogApplication;
import com.ncmmall.domain.business.goods.unit.GoodsUnit;
import com.ncmmall.domain.business.goods.unit.QGoodsUnit;
import com.ncmmall.mall.controller.securitys.shiro.Securitys;
import com.ncmmall.mall.params.PageParams;
import com.ncmmall.querychannel.QueryPage;
import com.ncmmall.querychannel.QueryPageRequest;
import com.ncmmall.querychannel.service.QueryChannelService;
import com.google.common.collect.ImmutableMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 商品单位管理
 *
 * @author likaikai
 * @date 2021-2-6 17:04:08
 */
@Controller
@RequestMapping(value = "/goods/unit")
public class GoodsUnitController {

    private final static Logger logger = LoggerFactory.getLogger(GoodsUnitController.class);

    @Autowired
    private QueryChannelService queryChannelService;

    @Autowired
    private GoodsUnitApplication unitApplication;

    @Autowired
    private OperationLogApplication logApplication;

    /**
     * 单位列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    private String list(Model model, PageParams pageParams, GoodsUnit unit) {
        QueryPageRequest pageRequest = new QueryPageRequest().setPageNo(pageParams.getPageNo());
        Map<String, Object> params = new HashMap<>();
        params.put("id", Securitys.currentUser().getId());
        String hql = builderSQL("select u from GoodsUnit u where u.merchant.id = :id ", params, unit);
        QueryPage<GoodsUnit> page = queryChannelService.findPage(hql, pageRequest.setParams(params), GoodsUnit.class);
        model.addAttribute("page", page).addAttribute("params", unit);
        return "merchant/goods/unit/list";
    }


    /**
     * 编辑单位信息
     */
    @RequestMapping(value = "/{id}/modify", method = RequestMethod.GET)
    private String modify(Model model, @PathVariable("id") long id) {
        GoodsUnit unit = queryChannelService.findOne(QGoodsUnit.goodsUnit.id.eq(id), GoodsUnit.class);
        model.addAttribute("_method", "PUT").addAttribute("unit", unit);
        return "merchant/goods/unit/form";

    }

    /**
     * 编辑单位信息
     */
    @RequestMapping(value = "/{id}/modify", method = RequestMethod.PUT)
    @ResponseBody
    private Map<String, Object> modify(HttpServletRequest request, GoodsUnit unit) {
        try {
            unitApplication.modify(unit);
            logApplication.eventOfMerchant(Securitys.currentUser(), "修改单位信息", Securitys.getIp(request));
            return ImmutableMap.of("status", true);
        } catch (Exception e) {
            logger.error("[编辑单位信息异常]", e);
            return ImmutableMap.of("status", false);
        }
    }

    /**
     * 添加单位页面
     */
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    private String create(Model model) {
        GoodsUnit unit = new GoodsUnit();
        model.addAttribute("_method", "POST").addAttribute("unit", unit);
        return "merchant/goods/unit/form";
    }

    /**
     * 添加单位
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    private Map<String, Object> create(HttpServletRequest request, GoodsUnit unit) {
        try {
            unit.setMerchant(Securitys.currentUser());
            unitApplication.create(unit);
            logApplication.eventOfMerchant(Securitys.currentUser(), "添加单位", Securitys.getIp(request));
            return ImmutableMap.of("status", true);
        } catch (Exception e) {
            logger.error("[添加单位信息异常]", e);
            return ImmutableMap.of("status", false);
        }
    }

    /**
     * 删除单位
     */
    @RequestMapping(value = "/remove", method = RequestMethod.DELETE)
    @ResponseBody
    private Map<String, Object> remove(HttpServletRequest request, @RequestHeader("X-Requested-With") String requestWith, @RequestParam("id") long id) {
        try {
            GoodsUnit unit = queryChannelService.findOne(QGoodsUnit.goodsUnit.id.eq(id), GoodsUnit.class);
            unitApplication.remove(unit);
            logApplication.eventOfMerchant(Securitys.currentUser(), "删除单位", Securitys.getIp(request));
        } catch (Exception e) {
            logger.error("[权限删除异常]", e);
            return ImmutableMap.of("status", false);
        }
        return ImmutableMap.of("status", true);

    }

    /**
     * 条件筛选
     */
    private String builderSQL(String sql, Map<String, Object> params, GoodsUnit goods) {
        if (goods.getStatus() > 0) {
            sql += " and u.status = :status ";
            params.put("status", goods.getStatus());
        } else {
            sql += " and u.status != 2 ";
        }
        return sql;
    }

}
